<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2017 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: pl125 <xskjs888@163.com>
// +----------------------------------------------------------------------

namespace api\wxapp\controller;

use api\wxapp\controller\ApiBaseController;
use think\Db;
use api\wxapp\model\User;
use api\wxapp\model\Integral;
use api\wxapp\model\Config;
class LoginController extends ApiBaseController
{
    public function index()
    {
        $params = $this->getParam();
        !isset($params['openid']) ? $this->error('参数缺失!!!!') : '';
        $info = User::where('openid',$params['openid'])->find();
        if($info){
            //查询当天登陆是否 已经获取过积分
            $map['time'] = date('Y-m-d',time());
            $map['type'] = 4;
            $map['uid'] = $info['id'];
            $find = Integral::where($map)->find();
            if(!$find){
                $this->scoreInc($info);
            }
            //查询  当前用户 有没有验证手机号码  绑定店铺
            if(!$info['phone']){
                return json(['code'=>300,'msg'=>'暂未绑定手机号码','data'=>$info['id']]);
            }
            if(!$info['shop_id']){
                return json(['code'=>400,'msg'=>'暂未绑定店铺','data'=>$info['id']]);
            }
            $this->success('请求成功!',$info);
        }else{
            //没有当前用户  添加用户
            $user = new User;
            $user->check($params) ? $this->error('参数缺失!!!') : '';
            $user->save($params);
            $this->scoreInc($user);
            return json(['code'=>300,'msg'=>'暂未绑定手机号码','data'=>$user->id]);
        }
    }
    //用户登陆增加积分
    private function scoreInc($info)
    {
        Db::startTrans();
        try{
            $data['uid'] = $info['id'];
            $score = Config::where('id',10)->value('value');
            $data['integral'] = $score;
            $data['addtime'] = $this->returnDateTime();
            $data['time'] = $this->returnDate();
            $data['type'] = 4;
            $data['status'] = 1;
            $data['remark'] = '登陆获取积分';
            $res1 = Integral::insert($data);
            if(!$res1){
                throw new \Exception('积分记录表更新失败');
            }
            //用户表增加积分
            $res2 = User::where('id',$info['id'])->setInc('integral',$score);
            if(!$res2){
                throw new \Exception('用户积分更新失败');
            }
            Db::commit(); //提交事务
        }catch(\Exception $e){
            Db::rollback(); //回滚事务
            $this->error($e->getMessage());
        }
    }

    //用户绑定店铺  手机号码
    public function binding()
    {
        $params = $this->getParam();
        $this->checkuid($params);
        switch($this->getMethod()){
            case 'POST':
                //绑定手机号码
                !isset($params['phone']) ? $this->error('参数缺失!!!!') : '';
                $res = User::where('id',$params['uid'])->update(array('phone'=>$params['phone']));
                if($res){
                    return json(['code'=>400,'msg'=>'手机号码绑定成功,请绑定店铺']);
                }else{
                    $this->error('绑定失败!!!!');
                }
            break;
            case 'PUT':
                //绑定店铺
                !isset($params['shop_id']) ? $this->error('参数缺失!!!!') : '';
                $res = User::where('id',$params['uid'])->update(array('shop_id'=>$params['shop_id']));
                if($res){
                    $this->success('绑定成功!!!!');;
                }else{
                    $this->error('绑定失败!!!!');
                }
            break;
        }
    }
}